package cn.iocoder.yudao.module.shop.info.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopEnterPackageDO;
import cn.iocoder.yudao.module.shop.info.service.ShopEnterPackageService;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterPackageRespVO;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterPackageSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 店铺套餐")
@RestController
@RequestMapping("/shop/enter-package")
@Validated
public class ShopEnterPackageController {

    @Resource
    private ShopEnterPackageService shopEnterPackageService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺入驻套餐")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Long> createEnterPackage(@Valid @RequestBody ShopEnterPackageSaveReqVO createReqVO) {
        return success(shopEnterPackageService.createEnterPackage(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新店铺入驻套餐")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> updateEnterPackage(@Valid @RequestBody ShopEnterPackageSaveReqVO updateReqVO) {
        shopEnterPackageService.updateEnterPackage(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除店铺入驻套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> deleteEnterPackage(@RequestParam("id") Long id) {
        shopEnterPackageService.deleteEnterPackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺入驻套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:enter-package:query')")
    public CommonResult<ShopEnterPackageRespVO> getEnterPackage(@RequestParam("id") Long id) {
        ShopEnterPackageDO enterPackage = shopEnterPackageService.getEnterPackage(id);
        return success(BeanUtils.toBean(enterPackage, ShopEnterPackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺入驻套餐分页")
    @PreAuthorize("@ss.hasPermission('shop:enter-package:query')")
    public CommonResult<PageResult<ShopEnterPackageRespVO>> getEnterPackagePage(@Valid PageParam pageReqVO) {
        PageResult<ShopEnterPackageDO> pageResult = shopEnterPackageService.getEnterPackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopEnterPackageRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得店铺入驻套餐列表")
    @PreAuthorize("@ss.hasPermission('shop:enter-package:query')")
    public CommonResult<List<ShopEnterPackageRespVO>> getEnterPackageList() {
        List<ShopEnterPackageDO> pageResult = shopEnterPackageService.getList();
        return success(BeanUtils.toBean(pageResult, ShopEnterPackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店铺入驻套餐 Excel")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnterPackageExcel(HttpServletResponse response) throws IOException {
        List<ShopEnterPackageRespVO> list = getEnterPackageList().getData();
        // 导出 Excel
        ExcelUtils.write(response, "店铺入驻套餐.xls", "数据", ShopEnterPackageRespVO.class, list);
    }

}
